package timing.ukulele.third.msgChannel.action.recall;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import timing.ukulele.third.exception.ThirdServiceException;
import timing.ukulele.third.msgChannel.model.RecallTaskInfo;
import timing.ukulele.third.msgChannel.param.RecallMessageParam;
import timing.ukulele.third.persistent.msg.MessageTemplate;
import timing.ukulele.third.service.msg.MessageTemplateService;
import java.util.Objects;

/**
 * 组装撤回参数
 * @author zh
 */
@Slf4j
@Service
public class RecallAssembleAction {

    @Autowired
    private MessageTemplateService messageTemplateService;

    public RecallTaskInfo process(RecallMessageParam recallMessageParam) {

        Long messageTemplateId = recallMessageParam.getMessageTemplateId();

        // 没有传入消息模板Id
        if (Objects.isNull(messageTemplateId)) {
            throw new ThirdServiceException("没有传入消息模板Id");
        }
        // 查询模板数据
        MessageTemplate messageTemplate = messageTemplateService.getById(messageTemplateId);
        if(messageTemplate == null) {
            throw new ThirdServiceException("找不到模板或模板已被删除");
        }

        RecallTaskInfo recallTaskInfo = RecallTaskInfo.builder()
                .messageTemplateId(messageTemplate.getId())
                .sendChannel(messageTemplate.getSendChannel())
                .sendAccountId(messageTemplate.getSendAccountId())
                .recallMessageId(recallMessageParam.getRecallMessageId())
                .build();
        return recallTaskInfo;
    }

}
